<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Response;

class AuthController extends Controller
{
    /**
     * 登录页面
     */
    public function getLogin()
    {
        return view('admin/login');
    }

    /**
     * 登录过程
     *
     * @param Request $request
     *
     * @return Response
     */
    public function postLogin(Request $request)
    {
         $credential = $request->only(['user', 'password', 'captcha']);

         $validator = \Validator::make($credential, ['user' => 'required', 'password' => 'required', 'captcha' => 'required|captcha']);

        if ($validator->fails()) {
            return redirect()->route('admin.login')->withErrors($validator)->withInput();
        }

        if (\Admin::attempt($credential)) {

            return redirect()->route('admin.dashboard');
        }

      return redirect()->route('admin.login')->withInput()->withErrors(trans('admin.login.login-fail'));
    }

    public function getLogout()
    {
        \Admin::logout();

        return redirect()->route('admin.login');
    }
}
